Lab 2 Functions 1
Goals
Hello
- Use basic Python functions and operators
- Run code step-by-step in Thonny
- Define custom functions that use
print
- Define custom functions which are fruitful
- Use functions with
turtle
graphics orwavesynth
audio (your choice)
Table of Contents
Big Questions
- What's the difference between
print
andreturn
?Show Answer
There are a few differences:
- The
return
value from a function can be used by other functions or code, or stored in a variable. Text printed usingprint
cannot be stored in a variable or used by other code. - A function can use
print
however much it likes, but it can only have one return value. - When you run a single line of code in the shell, both anything
that it prints using
print
and its result value are displayed. But when you run code in your file, only output created usingprint
will show up.
- The
- What is the first step when writing a custom function?
Show Answer
Before anything else, you should write your documentation string! This allows you to create a plan for what the function will do, as you describe it. Often, you will find that you either don't understand enough details to describe it, or you realize something about what your code needs to do as you describe it, both of which can save you a lot of time.